<?php
class LatestProjectCommentWidget extends Widget{
	//评论list
	public function render($data){
		//用户评论
		if($data['type'] == 'user'){
			import('ORG.Util.Page');
			$count = M()->query("select count(*) num from projectcomment p,users u where p.IsStudent='1' and p.UserId=u.UserId and p.ProjectId=".$data['projectid']." order by p.CreateDate desc");
			$Page = new Page($count[0]['num'],10);
			$show = $Page->show();
			$data['user'] = M()->query("select p.Content,p.UserName,p.UserId,p.OrgName,p.CreateDate,u.Avatar from projectcomment p,users u where p.IsStudent='1' and p.UserId=u.UserId and p.ProjectId=".$data['projectid']." order by p.CreateDate desc limit {$Page->firstRow},{$Page->listRows}");
			$data['page'] = $show;
			$content = $this->renderFile('UserComment',$data);
			return $content;
			//老师及教官评论
		}else if($data['type'] == 'teacher'){
			import('ORG.Util.Page');
			$count = M()->query("select count(*) num from projectcomment p,users u where p.IsStudent='0' and p.UserId=u.UserId and p.ProjectId=".$data['projectid']." order by p.CreateDate desc");
			$Page = new Page($count[0]['num'],5);
			$show = $Page->show();
			$data['teacher'] = M()->query("select p.Content,p.UserName,p.UserId,p.OrgName,p.CreateDate,u.Avatar,u.UserType from projectcomment p,users u where p.IsStudent='0' and p.UserId=u.UserId and p.ProjectId=".$data['projectid']." order by p.CreateDate desc limit {$Page->firstRow},{$Page->listRows}");
			$data['page'] = $show;
			
			$pid = $data['projectid'];
			//判断是否公开
			$project = M('Project')->where('Id='.$pid)->find();
			$data['project'] = $project;
			$limit = M('Projectcommentlimit')->where('ProjectId='.$pid)->select();
			$createuser = M()->query("select u.UserId from project p,team t,users u where t.Captain=u.UserId and p.TeamId=t.Id and p.Id=".$pid);
			//当前登录用户的uid
			$uid = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
			if(!empty($limit)){
				foreach($limit as $row){
					$permit[] = $row['UserId'];
				}
			}
			$permit[] = $createuser[0]['UserId'];
			if(in_array($uid,$permit)){
				$data['premit'] = 1;
			}else{
				$data['premit'] = -1;
			}
			
			$content = $this->renderFile('TeacherComment',$data);
			return $content;
		}
		
	}
}